package cn.kgc.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author 课工场
 * @date 2024/10/15
 * @description
 */
@RestController
@RequestMapping("student")
public class StudentController {

    // 注入spring中封装的可以发送http请求的 对象
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;


    @GetMapping("m1")
    public String  method1(){
        // 需要调用course服务   course m1  http请求  http://localhost:8540/course/m1   response
        // 如何实现 http请求发送
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject("http://"+getRandomHost()+"/course/m1 ", String.class);
        return "studentController  service ok  invoke  courseController  -->  "+info;
    }

    @GetMapping("m2")
    public String  method2(){
        // 需要调用course服务   course m1  http请求  http://localhost:8540/course/m1   response
        // 如何实现 http请求发送  target
        List<ServiceInstance> courseServices = discoveryClient.getInstances("COURSESERVICE");
        courseServices.forEach(courseService-> System.out.println(courseService.getUri()));
        Random random = new Random();
        int index = random.nextInt(courseServices.size());
        URI uri = courseServices.get(index).getUri();

        // 创建restTemplate对象
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject(uri+"/course/m1 ", String.class);

        return "studentController  service ok  invoke  courseController  -->  "+info;
    }

    @GetMapping("m3")
    public String  method3(){

        // 通过负载均衡对象实现目标服务的获取  默认通过轮循策略   restTemplate + ribbon
        URI uri = loadBalancerClient.choose("COURSESERVICE").getUri();
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject(uri+"/course/m1 ", String.class);
        return "studentController  service ok  invoke  courseController  -->  "+info;
    }


    @GetMapping("m4")
    public String  method4(){

        String info = restTemplate.getForObject("http://COURSESERVICE/course/m1", String.class);
        return "studentController  service ok  invoke  courseController  -->  "+info;
    }



    // 随机获取主机地址    1.健康状态无法控制  2.服务扩充无法实现
    public String getRandomHost(){
        // 主机地址
        List<String> hosts = Arrays.asList("localhost:8540", "localhost:8541", "localhost:8542");
        Random random = new Random();
        int index = random.nextInt(hosts.size());

        return hosts.get(index);
    }

}
